//
//  BKHistoryScoreView.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/1.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKHistoryScoreView.h"
#import "BKHistoryTableViewCell.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "BKHistoryViewModel.h"


static NSString *historyCell = @"HistoryCell";
@implementation BKHistoryScoreView

- (void)dealloc{

    _tableVIew.delegate = nil;
    _tableVIew.dataSource = nil;
}

- (void)awakeFromNib{
    
    _tableVIew.layer.cornerRadius = 5;
    _tableVIew.layer.borderColor = [[UIColor colorWithRed:0.529 green:0.867 blue:0.941 alpha:1.000]CGColor];
    _tableVIew.layer.borderWidth = .5;
    _tableVIew.separatorColor = [UIColor clearColor];
    _tableVIew.delegate = self;
    _tableVIew.dataSource = self;
    [_tableVIew registerNib:[UINib nibWithNibName:NSStringFromClass([BKHistoryTableViewCell class]) bundle:nil] forCellReuseIdentifier:historyCell];
    
    @weakify(self);
    [RACObserve(self, kaoquTextField.text) subscribeNext:^(id x) {
        @strongify(self);
        if ([x isKindOfClass:[NSString class]]&&[x length]>0) {
            [BKHistoryViewModel historScoreWithCid:self.cid kq:x success:^(NSDictionary *dic) {
                self.dataDic = dic;
                dispatch_queue_t mainQueue = dispatch_get_main_queue();
                dispatch_async(mainQueue, ^(void) {
                    [self.tableVIew reloadData];
                    
                });

            } failure:^(NSString *message) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                [alertView show];
            }];
        }
        
    }];
    
   
    

    
}

- (void)setDataDic:(NSDictionary *)dataDic
{

    _dataDic = dataDic;
    [_tableVIew reloadData];
}
- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder{

    self = [super awakeAfterUsingCoder:aDecoder];
    if (self) {
        
        
        
    }
    return self;
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    BKHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:historyCell];
    if (indexPath.row%2!=0) {
        cell.backgroundColor = [UIColor clearColor];
    }else{
        cell.backgroundColor = [UIColor colorWithRed:0.914 green:0.965 blue:0.980 alpha:1.000];
    }
    
    switch (indexPath.row) {
        case 0:
            cell.year.text = @"2014年";
            cell.high.text = _dataDic[@"14h"];
            cell.low.text = _dataDic[@"14l"];
            cell.aver.text = _dataDic[@"14a"];
            break;
        case 1:
            cell.year.text = @"2013年";
            cell.high.text = _dataDic[@"13h"];
            cell.low.text = _dataDic[@"13l"];
            cell.aver.text = _dataDic[@"13a"];
            break;
        case 2:
            cell.year.text = @"2012年";
            cell.high.text = _dataDic[@"12h"];
            cell.low.text = _dataDic[@"12l"];
            cell.aver.text = _dataDic[@"12a"];
            break;
        default:
            break;
    }
    
    return cell;
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *view = [[NSBundle mainBundle] loadNibNamed:@"HistoryHeaderView" owner:self options:nil][0];
    return view;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 34;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 34;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 1;
}
@end
